﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Yugi.Client.Card;
using ServerYuGi;
using System.Threading;
using ManagementEffect;
using ServerYuGi.PythonRegister;

namespace ServerYuGi.Helper
{
    public class BattleHelper
    {
        public static void Attack(bool check,SSupervisor super,int IDAttack, int IDDefense, YMSG TrangThaiPhongThu, ref SPlayer playerTanCong, ref SPlayer playerPhongThu
            , SCard CardAttack, SCard CardDefense)
        {
            if(!check)
            {
                Console.WriteLine("Stopped Monster Attack");
                return;
            }
            YMonsterCard CardTanCong = CardAttack.MyYBaseCard as YMonsterCard;
            YMonsterCard CardPhongThu = CardDefense.MyYBaseCard as YMonsterCard;

            super.flagManager.SetValue("isChangeDamage", false);
            super.flagManager.SetValue("DamAtt", CardTanCong.Damage );
            super.flagManager.SetValue("DefAtt", CardTanCong.Defend);
            super.flagManager.SetValue("DamDef", CardPhongThu.Damage);
            super.flagManager.SetValue("DefDef", CardPhongThu.Defend);
        

            //-----------Check Excute Damage Calculation------------//
            super.ExEffectManager.IDPlayerCheck = playerTanCong.ID;
            super.ExEffectManager.CheckPointNow = YCheckPoint.BF_DAMAGECALCULATION;
            super.ExEffectManager.Init();
            super.RevMSGmanager.isPlayerKoTrongLuot = true;
            List<YCheckPoint> checkpoint = new List<YCheckPoint> { YCheckPoint.BF_DAMAGECALCULATION };
            check = super.Check_EffectExcute(checkpoint, super.ExEffectManager.IDPlayerCheck, "api_manager,tancong_id,bitancong_id", super.api_manager, CardTanCong.ID, CardPhongThu.ID);
            super.RevMSGmanager.Init();
            //-----------------------------------------------------//

            List<byte> ArrayByteThayDoiDiem;
            switch (TrangThaiPhongThu)
            {
                #region Trang thai la phong thu la dung mo
                case YMSG.CARD_TRANGTHAI_DUNG_MO:
                    int SoDiemBiTru1 = CardTanCong.Damage - CardPhongThu.Damage;
                    if (SoDiemBiTru1 > 0)
                    {
                        playerPhongThu.SoDiem -= SoDiemBiTru1;
                        ArrayByteThayDoiDiem = BitConverter.GetBytes(playerPhongThu.SoDiem).ToList();
                        ArrayByteThayDoiDiem.Insert(0, (byte)playerPhongThu.ID);
                        Console.WriteLine("Xoa Bai " + IDDefense.ToString());
                        super.SendMessage_ANIMATION_CARD_TANCONG_ToAll(IDAttack, IDDefense);
                        Thread.Sleep(1000);

                        super.SendMessage_PLAYER_THAYDOIDIEM_ToAll(ArrayByteThayDoiDiem.ToArray());


                        super.SendMessage_CARD_XOABAI_ToAll(IDDefense);

                      
                        check = CheckExcuteXoaLaBai(super, check, playerPhongThu.ID, IDDefense);
                        if (!check)
                            return;
                        //-------------------------------------------------------//
                        XoaBai(super, CardDefense, ref playerPhongThu);
                        //super.EffectTurnManager.RemoveScript(IDDefense);
                        //super.pyregister.RemoveIDinStore(CardDefense);
                        //playerPhongThu.XoaLaBaiHySinh(CardDefense);

                    }
                    else
                    {
                        playerTanCong.SoDiem += SoDiemBiTru1;

                        if (SoDiemBiTru1 == 0)
                        {
                            Console.WriteLine("Xoa Bai" + IDDefense.ToString());
                            Console.WriteLine("Xoa Bai" + IDAttack.ToString());
                            ArrayByteThayDoiDiem = BitConverter.GetBytes(playerTanCong.SoDiem).ToList();
                            ArrayByteThayDoiDiem.Insert(0, (byte)playerTanCong.ID);


                           
                            super.SendMessage_ANIMATION_CARD_TANCONG_ToAll(IDAttack, IDDefense);
                            Thread.Sleep(1000);

                            super.SendMessage_PLAYER_THAYDOIDIEM_ToAll(ArrayByteThayDoiDiem.ToArray());


                            super.SendMessage_CARD_XOABAI_ToAll(IDAttack);


                            /*------------------Active Hy Sinh------------------------*/
                            //super.Excute_BF_HySinh(IDAttack);
                            check = CheckExcuteXoaLaBai(super, check, playerTanCong.ID, IDAttack);
                            if (!check)
                                return;
                            //--------------------------------------------------------//


                            //super.EffectTurnManager.RemoveScript(IDAttack);
                            //super.pyregister.RemoveIDinStore(CardAttack);
                            XoaBai(super, CardAttack, ref playerTanCong);

                            super.SendMessage_CARD_XOABAI_ToAll(IDDefense);

                            /*------------------Active Hy Sinh------------------------*/
                            //super.Excute_BF_HySinh(IDDefense);
                            check = CheckExcuteXoaLaBai(super, check, playerPhongThu.ID, IDDefense);
                            if (!check)
                                return;
                            //--------------------------------------------------------//

                            XoaBai(super, CardDefense, ref playerPhongThu);
                            //super.EffectTurnManager.RemoveScript(IDDefense);
                            //super.pyregister.RemoveIDinStore(CardDefense);
                            //playerTanCong.XoaLaBaiHySinh(CardAttack);
                            //playerPhongThu.XoaLaBaiHySinh(CardDefense);


                        }
                        else
                        {
                            Console.WriteLine("Xoa Bai" + IDAttack.ToString());
                            ArrayByteThayDoiDiem = BitConverter.GetBytes(playerTanCong.SoDiem).ToList();
                            ArrayByteThayDoiDiem.Insert(0, (byte)playerTanCong.ID);
                          
                            super.SendMessage_ANIMATION_CARD_TANCONG_ToAll(IDAttack, IDDefense);
                            Thread.Sleep(1000);
                            super.SendMessage_PLAYER_THAYDOIDIEM_ToAll(ArrayByteThayDoiDiem.ToArray());


                            super.SendMessage_CARD_XOABAI_ToAll(IDAttack);

                            /*------------------Active Hy Sinh------------------------*/
                            //super.Excute_BF_HySinh(IDAttack);
                            check = CheckExcuteXoaLaBai(super, check, playerTanCong.ID, IDAttack);
                            if (!check)
                                return;
                            //--------------------------------------------------------//

                            XoaBai(super, CardAttack, ref playerTanCong);
                            //super.EffectTurnManager.RemoveScript(IDAttack);
                            //super.pyregister.RemoveIDinStore(CardAttack);
                            //playerTanCong.XoaLaBaiHySinh(CardAttack);

                        }

                    }
                    super.KiemTraHetDiem();
                    break;
                #endregion

                #region Trang thai phong thu la nam mo
                case YMSG.CARD_TRANGTHAI_NAM_MO:
                    int SoDiemBiTru2 = CardTanCong.Damage - CardPhongThu.Defend;
                    if (SoDiemBiTru2 > 0)
                    {
                        Console.WriteLine("Xoa Bai" + IDDefense.ToString());                       
                        super.SendMessage_ANIMATION_CARD_TANCONG_ToAll(IDAttack, IDDefense);
                        Thread.Sleep(1000);
                        super.SendMessage_CARD_XOABAI_ToAll(IDDefense);

                        /*------------------Active Hy Sinh------------------------*/
                        //super.Excute_BF_HySinh(IDDefense);

                        check = CheckExcuteXoaLaBai(super, check, playerPhongThu.ID, IDDefense);
                        if (!check)
                            return;
                        //--------------------------------------------------------//

                        XoaBai(super, CardDefense, ref playerPhongThu);
                        //super.EffectTurnManager.RemoveScript(IDDefense);
                        //super.pyregister.RemoveIDinStore(CardDefense);
                        //playerPhongThu.XoaLaBaiHySinh(CardDefense);

                    }
                    else
                    {
                        playerTanCong.SoDiem += SoDiemBiTru2;
                        ArrayByteThayDoiDiem = BitConverter.GetBytes(playerTanCong.SoDiem).ToList();
                        ArrayByteThayDoiDiem.Insert(0, (byte)playerTanCong.ID);
                      
                        super.SendMessage_ANIMATION_CARD_TANCONG_ToAll(IDAttack, IDDefense);
                        Thread.Sleep(1000);
                        super.SendMessage_PLAYER_THAYDOIDIEM_ToAll(ArrayByteThayDoiDiem.ToArray());

                    }
                    super.KiemTraHetDiem();
                    break;
                #endregion

                #region Trang thai phong thu la nam up
                case YMSG.CARD_TRANGTHAI_NAM_UP:
                    int SoDiemBiTru3 = CardTanCong.Damage - CardPhongThu.Defend;
                    super.SendMessage_CARD_DOITRANGTHAI_XONG_ToID(playerPhongThu.ID, CardPhongThu.ID, YMSG.CARD_TRANGTHAI_NAM_MO);
                    CardDefense.TrangThai = YMSG.CARD_TRANGTHAI_NAM_MO;

                    //-----------------Check Checkpoint Monster Flip---------------------------------------//
                    super.ExEffectManager.IDPlayerCheck = super.TurnManager.IDPlayerTrongLuot;
                    super.ExEffectManager.CheckPointNow = YCheckPoint.MONSTER_FLIP;
                    super.ExEffectManager.Init();
                    super.RevMSGmanager.isPlayerKoTrongLuot = true;
                    checkpoint = new List<YCheckPoint> { YCheckPoint.MONSTER_FLIP };
                    check = super.Check_EffectExcute(checkpoint, super.ExEffectManager.IDPlayerCheck, "api_manager,flip_id", super.api_manager, CardDefense.MyYBaseCard.ID);
                    super.RevMSGmanager.Init();
                    //-----------------------------------------------------------------------------------//

                    if (SoDiemBiTru3 > 0)
                    {
                        Console.WriteLine("Xoa Bai" + IDDefense.ToString());                        
                        super.SendMessage_ANIMATION_CARD_TANCONG_ToAll(IDAttack, IDDefense);
                        Thread.Sleep(1000);


                        super.SendMessage_CARD_XOABAI_ToAll(IDDefense);

                        /*------------------Active Hy Sinh------------------------*/
                        

                        check = CheckExcuteXoaLaBai(super, check, playerPhongThu.ID, IDDefense);
                        if (!check)
                            return;
                        //--------------------------------------------------------//

                        XoaBai(super, CardDefense, ref playerPhongThu);
                        


                    }
                    else
                    {
                        playerTanCong.SoDiem += SoDiemBiTru3;
                        ArrayByteThayDoiDiem = BitConverter.GetBytes(playerTanCong.SoDiem).ToList();
                        ArrayByteThayDoiDiem.Insert(0, (byte)playerTanCong.ID);                        
                        super.SendMessage_ANIMATION_CARD_TANCONG_ToAll(IDAttack, IDDefense);
                        Thread.Sleep(1000);                        
                        super.SendMessage_PLAYER_THAYDOIDIEM_ToAll(ArrayByteThayDoiDiem.ToArray());

                    }
                    super.KiemTraHetDiem();
                    break;
                #endregion
            }

            if (super.flagManager.GetValue<bool>("isChangeDamage"))
            {
                CheckIsChangeDamageCal(super, ref CardTanCong,super.flagManager.GetValue<int>("DamAtt"),super.flagManager.GetValue<int>("DefAtt"));
                CheckIsChangeDamageCal(super, ref CardPhongThu, super.flagManager.GetValue<int>("DamDef"), super.flagManager.GetValue<int>("DefDef"));
            }


        }
       
        static void XoaBai(SSupervisor super,SCard Card,ref SPlayer playerTanCong)
        {
            playerTanCong.CheckXoaScript(super, Card);
            playerTanCong.XoaLaBaiHySinh(Card);
        }

        static void CheckIsChangeDamageCal(SSupervisor super,ref YMonsterCard card,int Damage,int Defense)
        {           
            if (Damage != card.Damage || Defense!=card.Defend)
            {
                List<byte> tmp = new List<byte>();
                tmp.Add((byte)card.ID);
                tmp.AddRange(BitConverter.GetBytes(Damage));
                card.Damage = Damage;
                super.SendMessage_CARD_SETCONG_ToAll(tmp.ToArray());
                tmp.Clear();
                tmp.Add((byte)card.ID);
                tmp.AddRange(BitConverter.GetBytes(Defense));
                card.Defend = Defense;
                super.SendMessage_CARD_SETTHU_ToAll(tmp.ToArray());
            }
        }

        static bool CheckExcuteXoaLaBai(SSupervisor super,bool check,int IDPlayerXoaBai,int IDLaXoaBai)
        {
            super.ExEffectManager.IDPlayerCheck = IDPlayerXoaBai;
            super.ExEffectManager.CheckPointNow = YCheckPoint.MONSTER_ENTER_GRAVEYARD;
            super.ExEffectManager.Init();
            super.RevMSGmanager.isPlayerKoTrongLuot = true;
            List<YCheckPoint> checkpoint = new List<YCheckPoint> { YCheckPoint.MONSTER_ENTER_GRAVEYARD };
            check = super.Check_EffectExcute(checkpoint, super.ExEffectManager.IDPlayerCheck, "api_manager,id_baixoa,is_byEffect", super.api_manager, IDLaXoaBai,false);
            super.RevMSGmanager.Init();
            return check;
        }

        

        public static void DirectAttack(bool isAttack,SSupervisor super, SCard cardTanCong, ref SPlayer player, ref SPlayer Other)
        {
            if (!isAttack)
            {
                Console.WriteLine("Stopped Monster Direct Attack");
                return;
            }
            foreach (SCard item in Other.SoBaiTrenBan)
            {
                if (item.MyYBaseCard.CardType == YType.Monster)
                {
                    Console.WriteLine("Tan Cong Truc Tiep Ko Hop Le");
                    return;
                }
            }
            Other.SoDiem -= ((YMonsterCard)cardTanCong.MyYBaseCard).Damage;
            byte[] ArrayByte = new byte[5];
            ArrayByte[0] = (byte)Other.ID;
            Buffer.BlockCopy(BitConverter.GetBytes(Other.SoDiem), 0, ArrayByte, 1, 4);


            super.SendMessage_ANIMATION_CARD_TANCONG_TRUCTIEP_ToID(player.ID, 1, cardTanCong.MyYBaseCard.ID);
            super.SendMessage_ANIMATION_CARD_TANCONG_TRUCTIEP_ToID(Other.ID, 0, cardTanCong.MyYBaseCard.ID);
            Thread.Sleep(1000);

            super.SendMessage_PLAYER_THAYDOIDIEM_ToAll(ArrayByte);            
                
            super.KiemTraHetDiem();
        }

        public static bool CheckBlock_TanCong(SPlayer player,int ID)
        {
            if (player.Block.DuocPhep_TanCong.Contains(ID))
                return true;
            return false;
        }
        public static bool CheckBlock_TanCongTrucTiep(SPlayer player)
        {
            if (player.Block.DuocPhep_TanCongTrucTiep)
                return true;
            return false;
        }
        
    }
}
